package com.orientation.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orientation.commom.Result;
import com.orientation.commom.exception.CustomException;
import com.orientation.entity.Building;
import com.orientation.entity.Room;
import com.orientation.mapper.RoomMapper;
import com.orientation.service.BuildingService;
import com.orientation.service.RoomService;
import com.orientation.vo.RoomBedVo;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 报到 前端控制器
 * </p>
 *
 * @author xuandian
 * @since 2022-3-9
 */
@RestController
@RequestMapping("/room")
public class RoomController {

    @Resource
    private RoomService roomService;
    @Resource
    private RoomMapper roomMapper;
    @Resource
    private BuildingService buildingService;

    @ApiOperation("添加")
    @PostMapping
    public ResponseEntity<Result> insertRoom(@RequestBody Room room) {
        roomService.insertRoom(room);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("修改")
    @PutMapping
    public boolean update(@RequestBody Room room) {
        Room one = roomMapper.selectOne(new LambdaQueryWrapper<Room>().eq(Room::getName, room.getName()).eq(Room::getBuildingId, room.getBuildingId()).eq(Room::getFloor, room.getFloor()));
        if (!ObjectUtils.isEmpty(one)) {
            throw new CustomException("18", "此房间已经存在");
        }
        return roomService.updateById(room);
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable String id) {
        Room one = roomMapper.selectOne(new LambdaQueryWrapper<Room>().eq(Room::getSid, id));

        Building building = buildingService.getOne(Wrappers.<Building>lambdaQuery().eq(StringUtils.hasLength(one.getBuildingId()), Building::getSid, one.getBuildingId()).eq(StringUtils.hasLength(one.getFloor()), Building::getFloor, one.getFloor()));

        building.setRoom(building.getRoom() == 1 ? 0 : building.getRoom() - 1);
        buildingService.updateById(building);
        return roomService.removeById(id);
    }

    @ApiOperation("查询单条")
    @GetMapping("/{id}")
    public Room findById(@PathVariable String id) {
        return roomService.getById(id);
    }

    @ApiOperation("查询所有")
    @GetMapping
    public List<Room> findAll() {
        return roomService.list();
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public IPage<Room> findPage(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return roomService.page(new Page<>(pageNum, pageSize));
    }

    @ApiOperation("房间信息查询")
    @GetMapping("/queryRoomBedVo")
    public ResponseEntity<Result> queryRoomBedVo(@RequestParam(required = false, defaultValue = "") String buildingId,
                                                 @RequestParam(required = false, defaultValue = "") String roomName,
                                                 @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                 @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Page<RoomBedVo> roomBedVoPage = roomService.queryRoomBedVo(pageNum, pageSize, buildingId, roomName);
        return ResponseEntity.ok(Result.success(roomBedVoPage));
    }

    @ApiOperation("按照类型查询楼栋房间")
    @GetMapping("/getRoomList")
    public ResponseEntity<Result> getBuildingByType(String buildingId) {
        List<Room> rooms = roomService.list(Wrappers.<Room>lambdaQuery().eq(Room::getBuildingId, buildingId));
        return ResponseEntity.ok(Result.success(rooms));

    }

}

